<template>
  <div class="app-container">
    <!-- 表格 -->
    <el-table v-loading="listLoading" :data="data" element-loading-text="加载数据中" border fit highlight-current-row>
      <el-table-column align="center" label="序号" width="60">
        <template slot-scope="scope">{{
          scope.$index + (currentPage - 1) * eachPage + 1
        }}</template>
      </el-table-column>
      <el-table-column label="头像" align="center" width="80">
        <template slot-scope="scope">
          <el-avatar shape="square" size="small" :src="scope.row.itemArticleComment.userImg" />
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" width="150">
        <template slot-scope="scope">{{ scope.row.itemArticleComment.userName }}</template>
      </el-table-column>
      <!-- <el-table-column label="评论文章" align="center" width="150">
        <template slot-scope="scope">{{ scope.row.blog.title }}</template>
      </el-table-column> -->
      <el-table-column label="评论内容" align="center">
        <template slot-scope="scope">{{ scope.row.itemArticleComment.commentCont }}</template>
      </el-table-column>
      <el-table-column label="评论日期" align="center" width="230">
        <template slot-scope="scope">{{ scope.row.itemArticleComment.commentTime }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="删除" placement="top" :hide-after="1500">
            <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="deleteComment(scope.row)" />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="查看所有回复" placement="top" :hide-after="1500">
            <el-button type="primary" icon="el-icon-s-comment" circle size="mini"
              @click="getCommentReplyFn(scope.row.itemArticleComment._id, scope.row.articleId)" />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination style="margin-top: 30px" background :page-size="eachPage" :page-sizes="[5, 10, 15, 20]"
        layout="prev, pager, next, total, ->,sizes, jumper" :total="count" :current-page.sync="pagerCurrentPage"
        @prev-click="prevClickHandle" @next-click="nextClickHandle" @current-change="currentChangeHandle"
        @size-change="handleSizeChange" />
    </div>
    <!-- 评论回复抽屉 -->
    <el-drawer title="所有回复" :visible.sync="drawer" direction="rtl">
      <ul class="infinite-list" style="overflow:auto">
        <div v-if="currentCommentReply && currentCommentReply.length > 0">
          <li v-for="(item, index) in currentCommentReply" :key="index" class="infinite-list-item">
            <el-tag closable type="success" @close="handleCloseTag(item.id)">
              {{ item.commentCont }}
            </el-tag>
          </li>
        </div>
        <el-empty v-else description="暂无回复" />
      </ul>
    </el-drawer>
  </div>
</template>

<script>
import { getBolgsComment, delBlogsComment, getCommentReply, delCommentReply } from '@/api/comment.js'
export default {
  data() {
    return {
      listLoading: false,
      currentPage: 1, // 当前页码
      eachPage: 5, // 每页显示条数
      totalPage: 0, // 总页数
      count: 0, // 数据总条数
      data: [], // 数据详情
      pagerCurrentPage: 1, // 分页栏当前页
      drawer: false,
      currentCommentReply: [],
      currentArticleId: "",// 当前评论的所属文章 id
      currentCommentBlCeid: ""
    }
  },
  // 一进来就需要获取数据
  created() {
    this.fetchData()
  },
  methods: {
    handleCloseTag(id) {
      this.$confirm('是否删除此条回复论信息,并连同删除该条评论下的全部留言?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delCommentReply(this.currentArticleId, this.currentCommentBlCeid, id).then((res) => {
            console.log(res, '删除成功')
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.drawer = false
          })
        })
        .catch((err) => {
          this.$message({
            type: 'info',
            message: err
          })
        })
    },
    // 获取数据
    fetchData() {
      this.listLoading = true
      getBolgsComment(this.currentPage, this.eachPage).then(({ data }) => {
        this.listLoading = false
        this.data = data.rows
        // 处理时间，显示正确的格式
        // for (const i of this.data) {
        //   i.createDate = formatDate(i.createDate)
        // }
        this.count = data.total // 计算总条数
        this.totalPage = Math.ceil(this.count / this.eachPage) // 总页数
        if (this.currentPage > this.totalPage && this.totalPage !== 0) {
          this.currentPage = this.totalPage
          this.fetchData()
        }
      })
    },
    // 删除留言
    deleteComment(data) {
      this.$confirm('是否删除此条评论信息,并连同删除该条评论下的全部留言?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delBlogsComment(data).then((res) => {
            console.log(res, '删除成功')
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.fetchData()
          })
        })
        .catch((err) => {
          this.$message({
            type: 'info',
            message: err
          })
        })
    },
    // 回去该留评论的所有回复
    getCommentReplyFn(id, articleId) {
      this.drawer = true
      this.currentArticleId = articleId
      this.currentCommentBlCeid = id
      getCommentReply(id).then((res) => {
        console.log(res, '获取成功')
        this.currentCommentReply = res.data.list
      })
    },
    // 点击上一页，下一页，不用重新发送请求
    // 因为当前页码改变会触发 currentChangeHandle 事件，然后就会自动发送请求了
    // 点击上一页
    prevClickHandle() {
      this.currentPage -= 1
    },
    // 点击下一页
    nextClickHandle() {
      this.currentPage += 1
    },
    // 点击页码
    currentChangeHandle(pageNum) {
      this.currentPage = ~~pageNum
      this.fetchData()
    },
    // 改变每页显示条数
    handleSizeChange(pagerNum) {
      this.currentPage = 1
      this.pagerCurrentPage = 1
      this.eachPage = ~~pagerNum
      this.fetchData()
    }
  }
}
</script>
